<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use DB;

class UserController extends ApiBaseController{

    public function test(){
        return $this->api_return(200);
    }

    /**
     * 用户注册
     *
     * @param Request $request
     * @return void
     */
    public function register(Request $request){
        $username = $request['username'];
        $password = $request['password'];
        $captcha = $request['captcha'];

        //验证参数
        if(empty($username) || empty($password) || empty($captcha)){
            return $this->api_return(402,[],"参数不能为空");
        }

        //判断用户是否已经注册过
        $result = DB::table("User")->where("Phone", $username)->first();
        if(!empty($result)){
            return $this->api_return(405,[],"用户已注册");
        }

        //验证验证码是否有效
        $code = Redis::get("SMS_CODE:".$username);
        if(empty($code) || $captcha != $code){
            return $this->api_return(403,[],"验证码错误");
        }
        Redis::del("SMS_CODE:".$username);

        //发送注册请求
        $message = [
            "msg" => "Register",
            "m_accounts" => $username,
            "m_openId" => md5($username . "liujianchen-123#"),
            "m_nickname" => $username,
            "m_regType" => "3",
            "m_sex" => "1",
            "m_imageUrl" => "",
            "m_type" => "1",
            "m_password" => md5($password),
        ];
        $response = sendMsgToServer($message);
        $response = json_decode($response);
        if(!isset($response->errorCode) || $response->errorCode){
            if($response->errorCode == 2){
                return $this->api_return(401, $response, "用户已注册");
            }
            return $this->api_return(401, $response, "注册失败");
        }
        return $this->api_return(200, [], "注册成功");
    }

    /**
     * 获取验证码
     *
     * @param Request $request
     * @return void
     */
    public function getCaptcha(Request $request){

        $mobile = $request['mobile'];
        //验证参数
        if(empty($mobile)){
            return $this->api_return(402,[],"参数不能为空");
        }
        $code = mt_rand(100000,999999);
        $result = sendAliyunSms($code, $mobile);
        if($result === true){
            return $this->api_return(200);
        }
        else{
            return $this->api_return(402,[
                "errormsg" => $result
            ],"短信发送失败");
        }
    }

    /**
     * 获取OpenId
     *
     * @param Request $request
     * @return void
     */
    public function getOpenId(Request $request){
        $username = $request['username'];
        $password = $request['password'];
        $loginType = $request['loginType'];

        //验证参数
        if(empty($username) || empty($password) || empty($loginType)){
            return $this->api_return(402,[],"参数不能为空");
        }

        $result = DB::table("User")->where("Phone", $username)->first();
        if(!isset($result)){
            return $this->api_return(406,[],"用户未注册");
        }

        if($loginType == 1){
            if(md5($password) != $result->Password){
                return $this->api_return(407,[],"密码错误");
            }
        }else if($loginType == 2){
            $code = Redis::get("SMS_CODE:".$username);
            if(empty($code) || $password != $code){
                return $this->api_return(407,[],"验证码错误");
            }
            Redis::del("SMS_CODE:".$username);
        }

        return $this->api_return(200,[
            "openId" => $result->OpenId
        ]);
    }

    /**
     * 密码重置
     *
     * @param Request $request
     * @return void
     */
    public function pwdReset(Request $request){
        $username = $request['username'];
        $password = $request['password'];
        $captcha = $request['captcha'];

        //验证参数
        if(empty($username) || empty($password) || empty($captcha)){
            return $this->api_return(402,[],"参数不能为空");
        }

        //判断用户是否已注册
        $result = DB::table("User")->where("Phone", $username)->first();
        if(!isset($result)){
            return $this->api_return(406,[],"用户未注册");
        }

        //验证验证码是否有效
        $code = Redis::get("SMS_CODE:".$username);
        if(empty($code) || $captcha != $code){
            return $this->api_return(403,[],"验证码错误");
        }
        Redis::del("SMS_CODE:".$username);

        DB::table("User")->where("Phone", $username)->update([
            "Password" => md5($password)
        ]);

        return $this->api_return(200);
    }

    /**
     * 充值
     *
     * @param Request $request
     * @return void
     */
    public function charge(Request $request){
        //发送注册请求
        $message = [
            "msg" => "charge",
            "userId" => $userId,
            "count" => $count,
        ];
        $response = sendMsgToServer($message);
        $response = json_decode($response);
        if(!isset($response->errorCode) || $response->errorCode){
            if($response->errorCode == 2){
                return $this->api_return(401, $response, "用户已注册");
            }
            return $this->api_return(401, $response, "注册失败");
        }
        return $this->api_return(200, [], "注册成功");
    }
}